var AsynchronousTest = AsyncTestCase('AsyncTestsSamp', {

    executeReader : function(query, db, callback, _args){
        
        /*
        setTimeout(function(){                    

            //setTimeout(function(){
            
                var onReady = function(){
                    console.log(query);
                    //try{                        
                     //   throw new Error('a');
                   // }            
                    //catch(e){
                        callback.apply(window, []);
                    //}
                };

                setTimeout(function(){
                    onReady();
                }, 100);
            
            //}, 100);

            
        
        }, 100);
        
        return;
        */
        
        _args = _args || [];
        db.transaction(function (transaction) {           
            
            transaction.executeSql(query, _args, function(){
                console.log(query);
                callback.apply(window, arguments);
            }, function (transaction, error) {
                console.log(query);
                //callback.apply(window, arguments);
                callback();
            });
            
            //callback();
        });
    
    }
    
   ,testSomethingComplicated : function(queue) {

        var crcDB = openDatabase2({
            shortName : 'crcInv1'
           ,dbName : 'crcInv1'
           ,version : '10'
           ,displayName : 'CRC Database'
           ,maxSize  : 1024 * 1024 * 5
           ,storageDriver : "IndexedDB"
           //,storageDriver : "LocalStorage"
           //,mode : 'sync'
        });
      
        var me = this;

        var query = "Create Table if not exists LoginSignature " +
                        "(LoginID text, StoreID text, LoginTime text, Signature text)";        
        queue.call('Step 1: query the database', function(callbacks) {
            var cb = callbacks.add(function(t, r_or_e){
                //console.log(r_or_e);                
                //assertTrue(r_or_e.code === 1);
                assertTrue(false);
            });
             //cb.apply(window, []);
            me.executeReader(query, crcDB, cb);
            //setTimeout(function(){
            //    cb(null, null);
                //cb.apply(window, [null, null]);
            //}, 5000);
        });
       
       
        /*
        var operations = [{
            op :   "DROP TABLE IF EXISTS LoginSignature"
        }, {
            op :   "Create Table LoginSignature (LoginID text, StoreID text, LoginTime text, Signature text)"
        }, {
            op :   "INSERT INTO LoginSignature VALUES(1, 1, 1, 1)"
           ,testFn : function(t, r_or_e){
                assertTrue(r_or_e.rowsAffected === 1);
            }
        }, {        
            op :   "SELECT * FROM LoginSignature"
           ,testFn : function(t, r_or_e){
                assertTrue(
                        r_or_e.rows && r_or_e.rows.length === 1
                 &&   r_or_e.rows.item(0).LoginID === 2
                );
           }           
        }];
        
        
        queue.call(function(callbacks){
        
            var _operations = operations, _me = me; //for quick reference
            
            var fn = function(op, jsTestCallback, _me){
            
                return function(asyncCallback){
                    _me.executeReader(op, crcDB, function(){
                        jsTestCallback.apply(window, arguments);
                        try {asyncCallback(); } catch(e){}
                    });
                }
                
            }, _testFns = [], emptyFn = function(){}, i, len, _operation;
            
            for(i = 0, len = _operations.length; i < len; i++){
                _operation = _operations[i];
                _testFns = _testFns.concat([fn(_operation.op, callbacks.add(_operation.testFn || emptyFn), _me)]);
            }
            
            async.series(_testFns, function(){});
            
        });
        */
    }
    
    
    ,_testSomethingComplicated2 : function(queue) {
  			
        var crcDB = openDatabase2({
            shortName : 'crcInv1'
           ,dbName : 'crcInv1'
           ,version : '10'
           ,displayName : 'CRC Database'
           ,maxSize  : 1024 * 1024 * 5
           ,storageDriver : "IndexedDB"
           //,storageDriver : "LocalStorage"
           //,mode : 'sync'
        });
      
        var me = this;
        
        /*        
        var query = "Create Table LoginSignature " +
                        "(LoginID text, StoreID text, LoginTime text, Signature text)";        
        queue.call('Step 1: query the database', function(callbacks) {
            var cb = callbacks.add(function(t, r_or_e){
                console.log(r_or_e);
                //assertEquals(1,0);
                assertTrue(r_or_e.code === 5);
            });
            
            me.executeReader(query, crcDB, cb);
        });
        */
            
        var operations = [{
            op :   "DROP TABLE IF EXISTS LoginSignature"
        }, {
            op :   "Create Table LoginSignature (LoginID text, StoreID text, LoginTime text, Signature text)"
        }, {
            op :   "INSERT INTO LoginSignature VALUES(1, 1, 1, 1)"
           ,testFn : function(t, r_or_e){
                assertTrue(r_or_e.rowsAffected === 1);
            }
        }, {        
            op :   "SELECT * FROM LoginSignature"
           ,testFn : function(t, r_or_e){
                assertTrue(
                        r_or_e.rows && r_or_e.rows.length === 1
                 &&   r_or_e.rows.item(0).LoginID === 2
                );
           }           
        }];
        
        
        queue.call(function(callbacks){
        
            var _operations = operations, _me = me; //for quick reference
            
            var fn = function(op, jsTestCallback, _me){
            
                return function(asyncCallback){
                    _me.executeReader(op, crcDB, function(){
                        jsTestCallback.apply(window, arguments);
                        asyncCallback();
                    });
                }
                
            }, _testFns = [], emptyFn = function(){}, i, len, _operation;
            
            for(i = 0, len = _operations.length; i < len; i++){
                _operation = _operations[i];
                _testFns = _testFns.concat([fn(_operation.op, callbacks.add(_operation.testFn || emptyFn), _me)]);
            }
            
            async.series(_testFns, function(){});
            
        });
      
    }
    
});